/**
 * Created by Administrator on 2017-08-16.
 */
/**
 * Created by Administrator on 2016-11-10.
 */
var id;
$.ajax({
    url: "apis/user/getPartnerInfo",
    type: 'post',
    data: JSON.stringify({}),
    dataType: 'json',
    async: false,
    contentType: 'application/json',
    success: function (res) {
        // console.log(res);
        // 富文本
        var contentURL = res.data.partnerContent;
        var data=$.ajax({
            url:"apis/" + contentURL,
            async:false
        });
        // console.log(data.responseText);
        var ueditor = UE.getEditor('editor', {});
        ueditor.addListener("ready", function () {
            // editor准备好之后才可以使用
            ueditor.setContent(data.responseText);
        });

        var companyName = res.data.companyName;
        var partnerName = res.data.partnerName;

        if (res.data.linkPhone == null || res.data.linkPhone == "null") {
            var inHtml = '<input name="GRP_PERNAME" type="text" class="linkphe" value="" />';
            $("#linktels").html(inHtml);
        } else if (res.data.linkPhone.indexOf(",") > 0) {
            var linkTel = res.data.linkPhone.split(",");
            var html = "";
            for (var i = 0; i < linkTel.length; i++) {
                html += '<input name="GRP_PERNAME" type="text" class="linkphe" value="' + linkTel[i] + '" />'
            }
            $("#linktels").html(html);
        } else {
            var linkTel = res.data.linkPhone;
            var inHtml = '<input name="GRP_PERNAME" type="text" class="linkphe" value="' + linkTel + '" />';
            $("#linktels").html(inHtml);
        }
        if (res.data.partnerQQ == null || res.data.partnerQQ == "null") {
            var qqHtml = '<input name="GRP_QQ" type="text" class="linkqq" value="" />';
            $("#linkqqs").html(qqHtml);
        } else if (res.data.partnerQQ.indexOf(",") > 0) {
            var adminQQ = res.data.partnerQQ.split(",");
            var htmlqq = "";
            for (var k = 0; k < adminQQ.length; k++) {
                htmlqq += '<input name="GRP_QQ" type="text" class="linkqq" value="' + adminQQ[k] + '" />'
            }
            $("#linkqqs").html(htmlqq);
        } else {
            var adminQQ = res.data.partnerQQ;
            var qqHtml = '<input name="GRP_QQ" type="text" class="linkqq" value="' + adminQQ + '" />';
            $("#linkqqs").html(qqHtml);
        }
        // var linkTel=res.data.linkPhone.split(",");
        // var adminQQ=res.data.partnerQQ.split(",");

        // 电话号码
        // var html="";
        // for(var i=0;i<linkTel.length;i++){
        //     html +='<input name="GRP_PERNAME" type="text" class="linkphe" value="'+ linkTel[i] +'" />'
        // }
        // $("#linktels").html(html);

        // QQ
        // var htmlqq="";
        // for(var k=0;k<adminQQ.length;k++){
        //     htmlqq +='<input name="GRP_QQ" type="text" class="linkqq" value="'+ adminQQ[k] +'" />'
        // }
        // $("#linkqqs").html(htmlqq);

        $("#GRP_NAME").val(companyName);
        $("#GRP_PERNAME").val(partnerName);
    }
})
//添加电话号码
$("#addTel").click(function () {
    var htmls = '<input name="GRP_PERNAME" type="text" class="linkphe" value="" />'
    $("#linktels").append(htmls);
})

//添加QQ
$("#addQQ").click(function () {
    var qq = '<input name="GRP_QQ" type="text" class="linkqq" value="" />'
    $("#linkqqs").append(qq);
});

$("#removeTel").click(function () {
    $(".linkphe:last-child").remove();
});
$("#removeQQ").click(function () {
    $(".linkqq:last-child").remove();
});
//修改资料
$("#btn_Save").click(function () {
    /* Act on the event */
    var phoneNums = "";
    var phoneNum = $(".linkphe");
    for (var j = 0; j < phoneNum.length; j++) {
        if (phoneNum[j].value && /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[3578]\d{9})$)/.test(phoneNum[j].value)) {

        } else {
            alert("请输入正确格式电话号码");
            return;
        }
        if (j == phoneNum.length - 1) {
            phoneNums += phoneNum[j].value;
        } else {
            phoneNums += phoneNum[j].value + ",";
        }
    }
    var qqNums = "";
    var qqNum = $(".linkqq");
    for (var h = 0; h < qqNum.length; h++) {
        if (qqNum[h].value) {

            if (h == qqNum.length - 1) {
                qqNums += qqNum[h].value;
            } else {
                qqNums += qqNum[h].value + ",";
            }
        }
    }
    var content = UE.getEditor('editor').getContent();
    if (content == "" || content == null) {
        var data = {
            "partnerContent": "",
            "linkPhone": phoneNums,
            "partnerQQ": qqNums,
            "partnerName": $("#GRP_PERNAME").val(),
            "companyName": $("#GRP_NAME").val()
        }
    } else {
        var data = {
            "partnerContent": content,
            "linkPhone": phoneNums,
            "partnerQQ": qqNums,
            "partnerName": $("#GRP_PERNAME").val(),
            "companyName": $("#GRP_NAME").val()
        }
    }
    // var data = {
    //     "linkPhone": phoneNums,
    //     "partnerQQ": qqNums,
    //     "partnerName": $("#GRP_PERNAME").val(),
    //     "companyName": $("#GRP_NAME").val()
    // };
    // console.log(data);
    // return;
    $.ajax({
        url: "apis/user/updateCompanySelf",
        type: 'post',
        data: {
            partner: JSON.stringify(data)
        },
        dataType: 'json',
        async: false,
        contentType: 'application/json',
        success: function (data) {
            // console.log(data);
            if (data.httpCode == 200) {
                alert("保存成功");
                window.location.reload();
            } else if(data.httpCode == 207){
                alert(data.msg);
            }
        }
    });
});